package com.sgb.knowledgeshare.weight;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.entity.TabelListEntity;
import com.sgb.knowledgeshare.view.adapter.LabelListAdapter;
import com.sgb.knowledgeshare.viewmodel.SharePublishActivityModel;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by：冯齐 on 2021/1/28 17:42
 * Description：
 */
public class ShareLabelDialog {

    private static RecyclerView listView;
    private static LabelListAdapter labelListAdapter;
    private static String labelName;
    private static String labelNo;
    private static List<TabelListEntity> tabellists;

    public static void showBottomDialog(SharePublishActivityModel viewmodel, List<TabelListEntity> tabellist, Context context) {
        labelNo = "";
        labelName = "";
        tabellists = tabellist;
        final Dialog dialog = new Dialog(context, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(context, R.layout.share_label_view, null);
        EditText labelEdittext = view.findViewById(R.id.label_edittext);
        listView = view.findViewById(R.id.listView);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.ActionSheetDialogAnimation);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
        //取消
        dialog.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //确定
        dialog.findViewById(R.id.determine).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tabellists!= null && tabellists.size() > 0) {
                        for (TabelListEntity tabelListEntity : tabellists) {
                            if (tabelListEntity.getLabelName().equals(labelEdittext.getText().toString().trim())){
                                MToast.showToast(context, "标签已存在，请重新添加标签！");
                                return;
                            }
                        }
                }
                if (!TextUtils.isEmpty(labelNo)) {
                    viewmodel.setLabelName(labelName, labelNo);
                } else {
                    if (!TextUtils.isEmpty(labelEdittext.getText().toString().trim())) {
                        viewmodel.setLabelName(labelEdittext.getText().toString().trim(), "");
                    } else {
                        MToast.showToast(context, "请输入标签名称");
                    }
                }
                dialog.dismiss();
            }
        });
        //输入监听事件
        labelEdittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(labelName)&&!TextUtils.equals(labelName, s.toString())){
                    labelNo="";
                }
                viewmodel.getLabelList(s.toString().trim());
            }
        });
        labelListAdapter = new LabelListAdapter(context, new ArrayList<>());
        listView.setLayoutManager(new LinearLayoutManager(context));
        listView.setAdapter(labelListAdapter);
        labelListAdapter.setOnItemClick(new AdapterOnItemClick<TabelListEntity>() {
            @Override
            public void onItemClick(TabelListEntity tabelListEntity, int position) {
                for (int i = 0; i < labelListAdapter.getDatas().size(); i++) {
                    if (i == position) {
                        labelListAdapter.getDatas().get(position).setSelected(!labelListAdapter.getDatas().get(position).isSelected());
                    } else {
                        labelListAdapter.getDatas().get(position).setSelected(false);
                    }
                }
                labelListAdapter.notifyDataSetChanged();
                if (tabelListEntity.getLabelName().length()<=10){
                    labelName = tabelListEntity.getLabelName();
                    labelNo = tabelListEntity.getLabelNo();
                    labelEdittext.setText(labelName);
                    labelEdittext.setSelection(labelName.length());
                }else {
                    MToast.showToast(context,"标签长度不能大于10");
                }
            }
        });
    }

    //返回的标签列表信息
    public static void setLabelList(List<TabelListEntity> label) {
        labelListAdapter.setDatas(label);
    }

    //更新用户标签数据信息
    public static void setUserTabList(List<TabelListEntity> datas) {
        tabellists = datas;
    }
}
